﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.Mvc;
using Lari.Configuration;
using Lari.Services.Data;
using Lari.Services.Implementation;
using Lari.Services.ViewModel;
using Lari.Web.Utility;

namespace Lari.Web.Controllers
{
    [Authorize]
    public class CoreMasterController : LariController
    {
        #region ctor and private member
        private readonly MasterManager _mgr;
        public CoreMasterController(MasterManager mgr)
        {
            _mgr = mgr;
        }
        #endregion

        #region Index
        public ActionResult Index()
        {
            return View();
        }
        #endregion

        #region Genders
        [AccessFilter(AccessConst.Master.Core.ManageGenders)]
        public ActionResult Genders()
        {
            var model = new List<GenderModel>();
            try
            {
                _mgr.Genders().ForEach(gender => model.Add(gender.ConvertToViewModel()));

            }
            catch (Exception ex)
            {
                ProcessException(MessageCode.Master.GenderFetchAll, ex, "MastersController_Genders");
            }

            return View(model);

        }
       
        [AccessFilter(AccessConst.Master.Core.ModifyGender)]
        public ActionResult ModifyGender(String id)
        {
            if (string.IsNullOrWhiteSpace(id))
            {
                AddErrorMessage(MessageCode.Generic.NoProperClick);
                return RedirectToAction(ActionConst.Masters.Common.Genders);
            }

            try
            {
                var gender = _mgr.Genders(id);
                if (gender == null)
                {
                   return NoDataFound;
                }
                return View(gender.ConvertToViewModel());
            }
            catch (Exception ex)
            {
                ProcessException(MessageCode.Master.GenderFetch, ex, "MastersController_ModifyGender");
            }
            return RedirectToAction(ActionConst.Masters.Common.Genders);
        }

        [AccessFilter(AccessConst.Master.Core.ModifyGender)]
        [HttpPost]
        public ActionResult ModifyGender(GenderModel model)
        {
            try
            {
                _mgr.Update(model);
                AddInfoMessage(MessageCode.Generic.DataSaved);
                return RedirectToAction(ActionConst.Masters.Common.Genders, ControllerConst.Masters.CoreMaster);
            }
            catch (Exception ex)
            {
                ProcessException(MessageCode.Master.UpdateGenderFailed, ex,
                                 moduleName: "MastersController_ModifyGender_post");
            }
            return View(model);
        }

        [AccessFilter(AccessConst.Master.Core.CreateGender)]
        public ActionResult CreateGender()
        {
            return View();
        }
        [AccessFilter(AccessConst.Master.Core.CreateGender)]
        [HttpPost]
        public ActionResult CreateGender(GenderModel model)
        {

            try
            {
                _mgr.Save(model);
                AddInfoMessage(MessageCode.Generic.DataSaved);
                return RedirectToAction(ActionConst.Masters.Common.Genders, ControllerConst.Masters.CoreMaster);
            }
            catch (Exception ex)
            {
                ProcessException(MessageCode.Master.SaveGenderFailed, ex,
                                 "MastersController_CreareGender_post");
            }
            return View(model);
        }
        #endregion

        #region Countries
        [AccessFilter(AccessConst.Master.Core.ListCountries)]
        public ActionResult Countries()
        {
            var model = new List<CountryModal>();
            try
            {
                _mgr.Countries().ForEach(country => model.Add(country.ConvertToViewModel()));

            }
            catch (Exception ex)
            {
                ProcessException(MessageCode.Master.CountryFetchAll, ex, "MastersController_Countries");
            }

            return View(model);

        }
        [AccessFilter(AccessConst.Master.Core.ModifyCountry)]
        public ActionResult ModifyCountry(string id)
        {
            if (string.IsNullOrWhiteSpace(id))
            {
                AddErrorMessage(MessageCode.Generic.NoProperClick);
                return RedirectToAction(ActionConst.Masters.Common.Countries);
            }

            try
            {
                var countruy = _mgr.Country(id);
                if (countruy == null)
                {
                    return NoDataFound;
                }
               
                return View(countruy.ConvertToViewModel());
            }
            catch (Exception ex)
            {
                ProcessException(MessageCode.Master.CountryFetch, ex, "MastersController_ModifyCountry");
            }
            return RedirectToAction(ActionConst.Masters.Common.Countries);


        }
        [AccessFilter(AccessConst.Master.Core.ModifyCountry)]
        [HttpPost]
        public ActionResult ModifyCountry(CountryModal model)
        {
            try
            {
                _mgr.Update(model);
                AddInfoMessage(MessageCode.Generic.DataSaved);
                return RedirectToAction(ActionConst.Masters.Common.Countries, ControllerConst.Masters.CoreMaster);
            }
            catch (Exception ex)
            {
                ProcessException(MessageCode.Master.UpdateCountryFailed, ex,
                                 "MastersController_ModifyGender_post");
            }
            return View(model);
        }


        [AccessFilter(AccessConst.Master.Core.CreateCountry)]
        public ActionResult CreateCountry()
        {
            return View();
        }
        [AccessFilter(AccessConst.Master.Core.CreateCountry)]
        [HttpPost]
        public ActionResult CreateCountry(CountryModal model)
        {
            try
            {
                _mgr.Save(model);
                AddInfoMessage(MessageCode.Generic.DataSaved);
                return RedirectToAction(ActionConst.Masters.Common.Countries, ControllerConst.Masters.CoreMaster);
            }
            catch (Exception ex)
            {
                ProcessException(MessageCode.Master.SaveCountryFailed, ex,
                                 "MastersController_CreateGender_post");
            }
            return View(model);

        }
        #endregion

        #region States
        [AccessFilter(AccessConst.Master.Core.ListStates)]
        public ActionResult States()
        {
            var model = new List<StateModel>();
            try
            {
                _mgr.States().ForEach(country => model.Add(country.ConvertToViewModel()));

            }
            catch (Exception ex)
            {
                ProcessException(MessageCode.Master.StateFetchAll, ex, "MastersController_States");
            }

            return View(model);
        }

        [AccessFilter(AccessConst.Master.Core.ModifyState)]
        public ActionResult ModifyState(string id)
        {
            if (string.IsNullOrWhiteSpace(id))
            {
                AddErrorMessage(MessageCode.Generic.NoProperClick);
                return RedirectToAction(ActionConst.Masters.Common.States);
            }

            try
            {
                var state = _mgr.State(id);
                if (state == null)
                {
                    return NoDataFound;
                }
                return View(state.ConvertToViewModel());
            }
            catch (Exception ex)
            {
                ProcessException(MessageCode.Master.StateFetch, ex, "MastersController_ModifyState");
            }
            return RedirectToAction(ActionConst.Masters.Common.Countries);
        }
        [AccessFilter(AccessConst.Master.Core.ModifyState)]
        [HttpPost]
        public ActionResult ModifyState(StateModel model)
        {
            try
            {
                _mgr.Update(model);
                AddInfoMessage(MessageCode.Generic.DataSaved);
                return RedirectToAction(ActionConst.Masters.Common.States, ControllerConst.Masters.CoreMaster);
            }
            catch (Exception ex)
            {
                ProcessException(MessageCode.Master.UpdateStateFailed, ex,
                                 "MastersController_ModifyState_post");
            }
            return View(model);
        }

        [AccessFilter(AccessConst.Master.Core.CreateState)]
        public ActionResult CreateState()
        {
            return View();
        }
        [AccessFilter(AccessConst.Master.Core.CreateState)]
        [HttpPost]
        public ActionResult CreateState(StateModel model)
        {
            try
            {
                _mgr.Save(model);
                AddInfoMessage(MessageCode.Generic.DataSaved);
                return RedirectToAction(ActionConst.Masters.Common.States, ControllerConst.Masters.CoreMaster);
            }
            catch (Exception ex)
            {
                ProcessException(MessageCode.Master.SaveStateFailed, ex,
                                 "MastersController_CreateState_post");
            }
            return View(model);
        }




        #endregion

        #region Religions
        [AccessFilter(AccessConst.Master.Core.ListReligion)]
        public ActionResult Religions()
        {
            var model = new List<ReligionModel>();
            try
            {
                _mgr.Religions().ForEach(rel => model.Add(rel.ConvertToViewModel()));

            }
            catch (Exception ex)
            {
                ProcessException(MessageCode.Master.ReligionFetchAll, ex, "MastersController_Religions");
            }

            return View(model);
        }


        [AccessFilter(AccessConst.Master.Core.ModifyReligion)]
        public ActionResult ModifyReligion(string id)
        {
            if (string.IsNullOrWhiteSpace(id))
            {
                AddErrorMessage(MessageCode.Generic.NoProperClick);
                return RedirectToAction(ActionConst.Masters.Common.Religions);
            }

            try
            {
                var religion = _mgr.Religion(id);
                if (religion == null)
                {
                    return NoDataFound;
                }
                return View(religion.ConvertToViewModel());
            }
            catch (Exception ex)
            {
                ProcessException(MessageCode.Master.ReligionFetch, ex, "MastersController_ModifyReligion");
            }
            return RedirectToAction(ActionConst.Masters.Common.Countries);
        }

        [AccessFilter(AccessConst.Master.Core.ModifyReligion)]
        [HttpPost]
        public ActionResult ModifyReligion(ReligionModel model)
        {
            try
            {
                _mgr.Update(model);
                AddInfoMessage(MessageCode.Generic.DataSaved);
                return RedirectToAction(ActionConst.Masters.Common.Religions, ControllerConst.Masters.CoreMaster);
            }
            catch (Exception ex)
            {
                ProcessException(MessageCode.Master.UpdateReligionFailed, ex,
                                 "MastersController_ModifyReligion_post");
            }
            return View(model);
        }

        [AccessFilter(AccessConst.Master.Core.CreateReligion)]
        public ActionResult CreateReligion()
        {
            return View();
        }
        [AccessFilter(AccessConst.Master.Core.CreateReligion)]
        [HttpPost]
        public ActionResult CreateReligion(ReligionModel model)
        {
            try
            {
                _mgr.Save(model);
                AddInfoMessage(MessageCode.Generic.DataSaved);
                return RedirectToAction(ActionConst.Masters.Common.Religions, ControllerConst.Masters.CoreMaster);
            }
            catch (Exception ex)
            {
                ProcessException(MessageCode.Master.SaveReligionFailed, ex,
                                 "MastersController_CreateReligion_post");
            }
            return View(model);
        }
        #endregion

        #region Marital Status
        [AccessFilter(AccessConst.Master.Core.ListMaritul)]
        public ActionResult MaritalStatus()
        {
            var model = new List<MaritalStatusModel>();
            try
            {
                _mgr.MaritalStatuses().ForEach(rel => model.Add(rel.ConvertToViewModel()));

            }
            catch (Exception ex)
            {
                ProcessException(MessageCode.Master.MaritalStatusFetchAll, ex, "MastersController_MaritalStatus");
            }

            return View(model);
        }

        [AccessFilter(AccessConst.Master.Core.ModifyMaritul)]
        public ActionResult ModifyMaritalStatus(string id)
        {
            if (string.IsNullOrWhiteSpace(id))
            {
                AddErrorMessage(MessageCode.Generic.NoProperClick);
                return RedirectToAction(ActionConst.Masters.Common.MaritalStatus);
            }

            try
            {
                var maritalStatus = _mgr.MaritalStatus(id);
                if (maritalStatus == null)
                {
                    return NoDataFound;
                }
                return View(maritalStatus.ConvertToViewModel());
            }
            catch (Exception ex)
            {
                ProcessException(MessageCode.Master.MaritalStatusFetch, ex, "MastersController_ModifyMaritalStatus");
            }
            return RedirectToAction(ActionConst.Masters.Common.MaritalStatus);
        }

        [AccessFilter(AccessConst.Master.Core.ModifyMaritul)]
        [HttpPost]
        public ActionResult ModifyMaritalStatus(MaritalStatusModel model)
        {
            try
            {
                _mgr.Update(model);
                AddInfoMessage(MessageCode.Generic.DataSaved);
                return RedirectToAction(ActionConst.Masters.Common.MaritalStatus, ControllerConst.Masters.CoreMaster);
            }
            catch (Exception ex)
            {
                ProcessException(MessageCode.Master.MaritalStatusUpdateFailed, ex,
                                 "MastersController_ModifyMaritalStatus_post");
            }
            return View(model);
        }


        [AccessFilter(AccessConst.Master.Core.CreateMaritul)]
        public ActionResult CreateMaritalStatus()
        {
            return View();
        }
        [AccessFilter(AccessConst.Master.Core.CreateMaritul)]
        [HttpPost]
        public ActionResult CreateMaritalStatus(MaritalStatusModel model)
        {
            try
            {
                _mgr.Save(model);
                AddInfoMessage(MessageCode.Generic.DataSaved);
                return RedirectToAction(ActionConst.Masters.Common.MaritalStatus, ControllerConst.Masters.CoreMaster);
            }
            catch (Exception ex)
            {
                ProcessException(MessageCode.Master.MaritalStatusSaveFailed, ex,
                                 "MastersController CreateMaritalStatus post");
            }
            return View(model);
        }

        #endregion

        #region Employee types
        [AccessFilter(AccessConst.Master.Core.EmployeeTypeList)]
        public ActionResult EmploeeTypes()
        {
            var model = new EmployeeTypeListModel { List = new List<EmployeeTypeModel>(), Edit = null, Insert = null };

            try
            {
                _mgr.EmployeeTypes().ForEach(et => model.List.Add(et.ConvertToViewModel()));

            }
            catch (Exception ex)
            {
                ProcessException(MessageCode.Master.EmployeeTypeFetchAll, ex, "MastersController EmploeeTypes");
            }

            return View(ActionConst.Masters.Common.EmploeeTypes, model);

        }

        public ActionResult EmploeeTypeModify()
        {
            return EmploeeTypes();
        }

        [AccessFilter(AccessConst.Master.Core.EmployeeTypeModify)]
        [HttpPost]
        public ActionResult EmploeeTypeModify(EmployeeTypeListModel model)
        {
            if (model.List == null)
            {
                model.List = new List<EmployeeTypeModel>();
            }
            model.Insert = null;
            try
            {
                _mgr.EmployeeTypes().ForEach(et => model.List.Add(et.ConvertToViewModel()));
                _mgr.Update(model.Edit);
                AddInfoMessage(MessageCode.Generic.DataSaved);
                return RedirectToAction(ActionConst.Masters.Common.EmploeeTypes);

            }
            catch (Exception ex)
            {
                ProcessException(MessageCode.Master.EmployeeTypeModify, ex, "MastersController EmploeeTypeModify");
            }
            return View(ActionConst.Masters.Common.EmploeeTypes, model);
        }

        [AccessFilter(AccessConst.Master.Core.EmployeeTypeCreate)]
        [HttpPost]
        public ActionResult EmploeeTypesCreate(EmployeeTypeListModel model)
        {
            if (model.List == null)
            {
                model.List = new List<EmployeeTypeModel>();
            }
            model.Edit = null;
            try
            {
                _mgr.EmployeeTypes().ForEach(et => model.List.Add(et.ConvertToViewModel()));
                _mgr.Save(model.Insert);
                AddInfoMessage(MessageCode.Generic.DataSaved);
                return RedirectToAction(ActionConst.Masters.Common.EmploeeTypes);

            }
            catch (Exception ex)
            {
                ProcessException(MessageCode.Master.EmployeeTypeSave, ex, "MastersController EmploeeTypesCreate");
            }
            return View(ActionConst.Masters.Common.EmploeeTypes, model);
        }

        public ActionResult EmploeeTypesCreate()
        {
            return EmploeeTypes();
        }
        #endregion

        #region Titles
        [AccessFilter(AccessConst.Master.Core.TitlesList)]
        public ActionResult Titles()
        {
            var model = new TitleListModel { List = new List<TitleModel>(), Edit = null, Insert = null };
            try
            {
                _mgr.Titles().ForEach(et => model.List.Add(et.ConvertToViewModel()));

            }
            catch (Exception ex)
            {
                ProcessException(MessageCode.Master.TitleFetchAll, ex, "MastersController Titles");
            }

            return View(ActionConst.Masters.Common.Titles, model);
        }

        public ActionResult TitleModify()
        {
            return Titles();
        }

        [HttpPost]
        [AccessFilter(AccessConst.Master.Core.TitleModify)]
        public ActionResult TitleModify(TitleListModel model)
        {
            if (model.List == null)
            {
                model.List = new List<TitleModel>();
            }
            model.Insert = null;
            try
            {
                _mgr.Titles().ForEach(et => model.List.Add(et.ConvertToViewModel()));
                _mgr.Update(model.Edit);
                AddInfoMessage(MessageCode.Generic.DataSaved);
                return RedirectToAction(ActionConst.Masters.Common.Titles);

            }
            catch (Exception ex)
            {
                ProcessException(MessageCode.Master.TitleModifyFailed, ex, "MastersController TitleModify post");
            }
            return View(ActionConst.Masters.Common.Titles, model);
        }

        public ActionResult TitleCreate()
        {
            return Titles();
        }

        [HttpPost]
        [AccessFilter(AccessConst.Master.Core.TitleCreate)]
        public ActionResult TitleCreate(TitleListModel model)
        {
            if (model.List == null)
            {
                model.List = new List<TitleModel>();
            }
            model.Edit = null;
            try
            {
                _mgr.Titles().ForEach(et => model.List.Add(et.ConvertToViewModel()));
                _mgr.Save(model.Insert);
                AddInfoMessage(MessageCode.Generic.DataSaved);
                return RedirectToAction(ActionConst.Masters.Common.Titles);

            }
            catch (Exception ex)
            {
                ProcessException(MessageCode.Master.TitleSaveFailed, ex, "MastersController TitleCreate post");
            }
            return View(ActionConst.Masters.Common.Titles, model);
        }
        #endregion

        #region Bloodgroups
        [AccessFilter(AccessConst.Master.Core.BloodgroupList)]
        public ActionResult Bloodgroups()
        {

            var model = new BloodgroupListModel { List = new List<BloodgroupModel>(), Edit = null, Insert = null };
            try
            {
                _mgr.Bloodgroups().ForEach(et => model.List.Add(et.ConvertToViewModel()));

            }
            catch (Exception ex)
            {
                ProcessException(MessageCode.Master.BloodgroupFetchAll, ex, "MastersController Bloodgroups");
            }
            return View(ActionConst.Masters.Common.Bloodgroups, model);
        }
        public ActionResult BloodgroupModify()
        {
            return Bloodgroups();
        }
        [HttpPost]
        [AccessFilter(AccessConst.Master.Core.BloodgroupModify)]
        public ActionResult BloodgroupModify(BloodgroupListModel model)
        {
            if (model.List == null)
            {
                model.List = new List<BloodgroupModel>();
            }
            model.Insert = null;
            try
            {
                _mgr.Bloodgroups().ForEach(et => model.List.Add(et.ConvertToViewModel()));
                _mgr.Update(model.Edit);
                AddInfoMessage(MessageCode.Generic.DataSaved);
                return RedirectToAction(ActionConst.Masters.Common.Bloodgroups);

            }
            catch (Exception ex)
            {
                ProcessException(MessageCode.Master.BloodgroupModifyFailed, ex, "MastersController BloodgroupModify post");
            }
            return View(ActionConst.Masters.Common.Bloodgroups, model);
        }
        public ActionResult BloodgroupCreate()
        {
            return Bloodgroups();
        }
        [HttpPost]
        [AccessFilter(AccessConst.Master.Core.BloodgroupCreate)]
        public ActionResult BloodgroupCreate(BloodgroupListModel model)
        {
            if (model.List == null)
            {
                model.List = new List<BloodgroupModel>();
            }
            model.Edit = null;
            try
            {
                _mgr.Bloodgroups().ForEach(et => model.List.Add(et.ConvertToViewModel()));
                _mgr.Save(model.Insert);
                AddInfoMessage(MessageCode.Generic.DataSaved);
                return RedirectToAction(ActionConst.Masters.Common.Bloodgroups);

            }
            catch (Exception ex)
            {
                ProcessException(MessageCode.Master.BloodgroupSavedFailed, ex, "MastersController BloodgroupCreate post");
            }
            return View(ActionConst.Masters.Common.Bloodgroups, model);
        }

        #endregion

        #region Designation
        [AccessFilter(AccessConst.Master.Core.DesignationsList)]
        public ActionResult Designations()
        {
            var model = new DesignationListModel { List = new List<DesignationModel>(), Edit = null, Insert = null };
            try
            {
                _mgr.Designations().ForEach(et => model.List.Add(et.ConvertToViewModel()));

            }
            catch (Exception ex)
            {
                ProcessException(MessageCode.Master.DesignationFetchAll, ex, "MastersController Designations");
            }
            return View(ActionConst.Masters.Common.Designations, model);
        }

        public ActionResult DesignationModif()
        {
            return Designations();
        }

        public ActionResult DesignationCreate()
        {
            return Designations();
        }

        [AccessFilter(AccessConst.Master.Core.DesignationModify)]
        [HttpPost]
        public ActionResult DesignationModify(DesignationListModel model)
        {
            if (model.List == null)
            {
                model.List = new List<DesignationModel>();
            }
            model.Insert = null;
            try
            {
                _mgr.Designations().ForEach(et => model.List.Add(et.ConvertToViewModel()));
                _mgr.Update(model.Edit);
                AddInfoMessage(MessageCode.Generic.DataSaved);
                return RedirectToAction(ActionConst.Masters.Common.Designations);

            }
            catch (Exception ex)
            {
                ProcessException(MessageCode.Master.DesignationModifyFailed, ex, "MastersController DesignationModif post");
            }
            return View(ActionConst.Masters.Common.Designations, model);
        }

        [AccessFilter(AccessConst.Master.Core.DesignationCreate)]
        [HttpPost]
        public ActionResult DesignationCreate(DesignationListModel model)
        {
            if (model.List == null)
            {
                model.List = new List<DesignationModel>();
            }
            model.Edit = null;
            try
            {
                _mgr.Designations().ForEach(et => model.List.Add(et.ConvertToViewModel()));
                _mgr.Save(model.Insert);
                AddInfoMessage(MessageCode.Generic.DataSaved);
                return RedirectToAction(ActionConst.Masters.Common.Designations);

            }
            catch (Exception ex)
            {
                ProcessException(MessageCode.Master.DesignationSavedFailed, ex, "MastersController DesignationCreate post");
            }
            return View(ActionConst.Masters.Common.Designations, model);
        }
        #endregion

        #region Department designation and specialization
        [AccessFilter(AccessConst.Master.Core.DepartmentList)]
        public ActionResult Departments()
        {
            var model = new DepartmentListModel { List = new List<DepartmentModel>(), Edit = null, Insert = null };
            try
            {
                _mgr.Departments().ForEach(et => model.List.Add(et.ConvertToViewModel()));

            }
            catch (Exception ex)
            {
                ProcessException(MessageCode.Master.DepartmentFetchAll, ex, "MastersController Department");
            }
            return View(ActionConst.Masters.Common.Departments, model);
        }

        public ActionResult DepartmentsModify()
        {
            return Departments();
        }
        public ActionResult DepartmentsCreate()
        {
            return Departments();
        }

        [HttpPost]
        [AccessFilter(AccessConst.Master.Core.DepartmentModify)]
        public ActionResult DepartmentsModify(DepartmentListModel model)
        {
            if (model.List == null)
            {
                model.List = new List<DepartmentModel>();
            }
            model.Insert = null;
            try
            {
                _mgr.Departments().ForEach(et => model.List.Add(et.ConvertToViewModel()));
                _mgr.Update(model.Edit);
                AddInfoMessage(MessageCode.Generic.DataSaved);
                return RedirectToAction(ActionConst.Masters.Common.Departments);

            }
            catch (Exception ex)
            {
                ProcessException(MessageCode.Master.DepartmentModifyFailed, ex, "MastersController DepartmentsModify post");
            }
            return View(ActionConst.Masters.Common.Departments, model);
        }

        [HttpPost]
        [AccessFilter(AccessConst.Master.Core.DepartmentCreate)]
        public ActionResult DepartmentsCreate(DepartmentListModel model)
        {
            if (model.List == null)
            {
                model.List = new List<DepartmentModel>();
            }
            model.Edit = null;
            try
            {
                _mgr.Departments().ForEach(et => model.List.Add(et.ConvertToViewModel()));
                _mgr.Save(model.Insert);
                AddInfoMessage(MessageCode.Generic.DataSaved);
                return RedirectToAction(ActionConst.Masters.Common.Departments);

            }
            catch (Exception ex)
            {
                ProcessException(MessageCode.Master.DepartmentSavedFailed, ex, "MastersController DepartmentsCreate post");
            }
            return View(ActionConst.Masters.Common.Departments, model);
        }


        [AccessFilter(AccessConst.Master.Core.DepDesignation, true)]
        public ActionResult DepDesignation(int id)
        {
            var model = new DepartmentDesignationModel();
            try
            {
                model = _mgr.Department(id).ConvertToDesignationMappingModel();
                if (model.Designations != null && model.Designations.Count > 0)
                {
                    _mgr.Designations().ForEach(d =>
                        {
                            if (!model.Designations.Exists(p => p.DesignationId == d.Id))
                                model.Designations.Add(new DepDesignationMappingModel { DesignationName = d.Name, DesignationId = d.Id, IsMapped = false });

                        });
                }

            }
            catch (Exception ex)
            {
                ProcessException(MessageCode.Master.DepDesignationFetch, ex, "MastersController DepDesignation");
            }
            return PartialView(model);
        }

        [HttpPost]
        [AccessFilter(AccessConst.Master.Core.DepDesignationModify, true)]
        public ActionResult DepDesignationModify(DepartmentDesignationModel model)
        {
            try
            {
                if (model.Designations == null || model.Designations.Count <= 0)
                {
                    model.Designations = new List<DepDesignationMappingModel>();
                    _mgr.Designations().ForEach(d => model.Designations.Add(new DepDesignationMappingModel { DesignationId = d.Id, DesignationName = d.Name, IsMapped = false }));
                    return PartialView(ActionConst.Masters.Common.DepDesignation, model);

                }

                if (model.Designations != null && model.Designations.Count > 0)
                {
                    if (!model.Designations.Exists(m => m.IsMapped))
                    {
                        AddErrorMessage(MessageCode.Master.NoDesigignationMapped);
                        return PartialView(ActionConst.Masters.Common.DepDesignation, model);
                    }
                    _mgr.Update(model);
                    AddErrorMessage(MessageCode.Generic.DataSaved);
                    return RedirectToAction(ActionConst.Masters.Common.DepDesignation, new { id = model.DepartmentId });



                }

            }
            catch (Exception ex)
            {

                ProcessException(MessageCode.Master.DesignationFetchAll, ex, "MastersController DepDesignationModify post");
            }

            return PartialView(ActionConst.Masters.Common.DepDesignation, model);
        }

        [AccessFilter(AccessConst.Master.Core.DepDesignationDelete)]
        public ActionResult DepDesignationDelete(int id)
        {
            try
            {
                _mgr.RemoveDesignationMapping(id);
            }
            catch (Exception ex)
            {
                ProcessException(MessageCode.Master.DepDesignationDelete, ex, "MastersController DepDesignation");
            }

            return RedirectToAction(ActionConst.Masters.Common.Departments);
        }

        [AccessFilter(AccessConst.Master.Core.SpecializationList, true)]
        public ActionResult Specialization(int id)
        {
            var model = new SpecializationListModel { ActionMode = EditingMode.View };
            try
            {
                var dep = _mgr.Department(id, true);
                if (dep != null)
                {
                    model.DepartmentId = dep.Id;
                    model.DepartmentName = dep.Name;
                    model.Specializations = new List<SpecializationModel>();
                    foreach (var spc in dep.Specializations)
                    {
                        model.Specializations.Add(new SpecializationModel { DepartmentID = dep.Id, ID = spc.ID, Name = spc.Name });
                    }
                }
            }
            catch (Exception ex)
            {
                ProcessException(MessageCode.Master.SpecializationFetch, ex, "CoreMastersController Specialization");
            }

            return PartialView(model);
        }

        [HttpPost]
        [AccessFilter(AccessConst.Master.Core.SpecializationCreate, true)]
        public ActionResult SpecializationAdd(SpecializationListModel model)
        {
            model.ActionMode = EditingMode.Insert;
            return PartialView(ActionConst.Masters.Common.Specialization, model);
        }

        [HttpPost]
        [AccessFilter(AccessConst.Master.Core.SpecializationCreate, true)]
        public ActionResult SpecializationAddSave(SpecializationListModel model)
        {
            try
            {
                _mgr.SaveSpecialization(model.DepartmentId, model.SpecializationName);
                AddInfoMessage(MessageCode.Generic.DataSaved);
                return RedirectToAction(ActionConst.Masters.Common.Specialization, new { id = model.DepartmentId });
            }
            catch (Exception ex)
            {
                ProcessException(MessageCode.Master.SpecializationSaveFailed, ex, "CoreMastersController SpecializationAddSave");
            }

            model.ActionMode = EditingMode.Insert;
            return PartialView(ActionConst.Masters.Common.Specialization, model);
        }

        [AccessFilter(AccessConst.Master.Core.SpecializationModify, true)]
        public ActionResult SpecializationModify(int id)
        {
            var model = new SpecializationListModel { ActionMode = EditingMode.Edit };
            try
            {
                var spc = _mgr.Specilization(id);
                if (spc != null)
                {
                    model.DepartmentId = spc.DepartmentID;
                    model.DepartmentName = spc.Department.Name;
                    model.SpecializationName = spc.Name;
                    model.OldSpecializationName = spc.Name;
                    model.SpecializationsId = spc.ID;
                }
            }
            catch (Exception ex)
            {
                ProcessException(MessageCode.Master.SpecializationFetch, ex, "CoreMastersController SpecializationModify");
            }

            return PartialView(ActionConst.Masters.Common.Specialization, model);
        }

        [AccessFilter(AccessConst.Master.Core.SpecializationModify, true)]
        public ActionResult SpecializationModifySave(SpecializationListModel model)
        {
            try
            {
                _mgr.ModifySpcealization(model.SpecializationName, model.SpecializationsId, model.DepartmentId);
                AddInfoMessage(MessageCode.Generic.DataSaved);
                return RedirectToAction(ActionConst.Masters.Common.Specialization, new { id = model.DepartmentId });
            }
            catch (Exception ex)
            {
                ProcessException(MessageCode.Master.SpecializationSaveFailed, ex, "CoreMastersController SpecializationModifySave");
            }
            model.ActionMode = EditingMode.Edit;
            return PartialView(ActionConst.Masters.Common.Specialization, model);
        }

        [AccessFilter(AccessConst.Master.Core.SpecializationDelete, true)]
        public ActionResult SpecializationDelete(int id)
        {

            try
            {
                var departmentId = _mgr.SpecilizationDelete(id);
                return RedirectToAction(ActionConst.Masters.Common.Specialization, new { id = departmentId });
            }
            catch (Exception ex)
            {
                ProcessException(MessageCode.Master.SpecializationDeleteFailed, ex, "CoreMastersController SpecializationDelete");
            }

            return RedirectToAction(ActionConst.Masters.Common.Specialization, new { id = 1 });
        }
        [HttpGet]
        public JsonResult GetDesignation(int id)
        {
            var al = new ArrayList();
            try
            {
                var dep = _mgr.Department(id);
                if (dep != null && dep.DepDesignationMappings != null && dep.DepDesignationMappings.Count > 0)
                {
                    foreach (var desi in dep.DepDesignationMappings)
                    {
                        al.Add(new { Value = desi.Designation.Id, Text = desi.Designation.Name });
                    }
                    return Json(al, JsonRequestBehavior.AllowGet);
                }
                
            }
            catch (Exception ex)
            {
                ProcessException(MessageCode.Master.DesignationFetchAll, ex, "MastersController GetDesignation Json");
            }
            return Json(al, JsonRequestBehavior.AllowGet);
        }

        #endregion

    }
}
